package com.youmu.shop.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.exception.ParamException;
import com.result.Result;
import com.result.ResultUtils;
import com.youmu.shop.context.utils.UserContext;
import com.youmu.shop.coupon.bean.Coupon;
import com.youmu.shop.coupon.service.CouponService;
import com.youmu.shop.shop.dao.ShopDao;
import com.youmu.shop.system.bean.MemberRank;
import com.youmu.shop.system.vo.UserInfo;
import com.youmu.shop.user.service.UserService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/user/remote/")
public class UserController {
	
	@Resource
    private UserService userService;
    
	@Autowired
	private ShopDao shopDao;
	
	@Autowired
	private CouponService couponService;
	
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "获取用户信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long", paramType = "query")
	})
   	@RequestMapping(value = "findUserInfo",method=RequestMethod.GET)
   	public Result<UserInfo> findUserInfo(Long userId) {
		if(userId == null){
			throw new ParamException("userId不能为空");
		}
       	UserInfo userInfo = userService.findUserInfo(userId);
       	return ResultUtils.createDefResult(userInfo);
   	}
	
	/**
	 * 微信端验证测试接口
	 * @param response
	 * @param echostr
	 */
	@RequestMapping(value = "weiXinTest")
	public void weiXinTest(HttpServletResponse response, String echostr){
		try {
			response.getWriter().print(echostr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检查用户是否拥有新的活动优惠券领取
	 * @return
	 */
	@RequestMapping(value = "checkUserHasNewCoupon",method = RequestMethod.GET)
	public Object checkUserHasNewCoupon(HttpServletRequest request){
		UserInfo userInfo = UserContext.getUserInfo();
		long userId = userInfo.getId();
        List<Coupon> coupons = userService.checkUserHasNewCoupon(userId);
		List<Coupon> returnList = new ArrayList<Coupon>();
		Map<String, Object> codeMap = new HashMap<String, Object>();
		for (Coupon couponVO : coupons) {
			codeMap.put("couponId", couponVO.getId());
			codeMap.put("memberId", userId);
			if(couponService.selectCouponCodeByCouponIdAndMemberId(codeMap) == null){
				returnList.add(couponVO);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if(returnList.size() > 0){
			map.put("hasActivityCoupon", 1);
		}else{
			map.put("hasActivityCoupon", 0);
		}
		map.put("userId", userId);
		return ResultUtils.createDefResult(map);
	}
	
	/**
	 * 查询用户等级对应的活动优惠券
	 * @return
	 */
	@RequestMapping(value = "selectActivityCoupons", method = RequestMethod.GET)
	public Object selectActivityCoupons(){
		UserInfo userInfo = UserContext.getUserInfo();
		long userId = userInfo.getId();
        List<Coupon> coupons = userService.checkUserHasNewCoupon(userId);
		List<Coupon> returnList = new ArrayList<Coupon>();
		Map<String, Object> codeMap = new HashMap<String, Object>();
		for (Coupon couponVO : coupons) {
			codeMap.put("couponId", couponVO.getId());
			codeMap.put("memberId", userId);
			if(couponService.selectCouponCodeByCouponIdAndMemberId(codeMap) == null){
				returnList.add(couponVO);
			}
		}
		return ResultUtils.createDefResult(coupons);
	}
	
	/**
	 * 用户点击确认领取后将优惠券与用户绑定
	 * @return
	 */
	@RequestMapping(value = "receiveCoupons", method = RequestMethod.GET)
	public Object receiveCoupons(){
		UserInfo userInfo = UserContext.getUserInfo();
		long userId = userInfo.getId();
        List<Coupon> coupons = userService.checkUserHasNewCoupon(userId);
		for (Coupon couponVO : coupons) {
			couponService.saveCouponForUser(couponVO.getId(), userId);
		}
		return ResultUtils.createDefResult(null);
	}
}
